package com.gen.velocity.controller;

import com.gen.velocity.aop.AnnotationPrint;
import com.gen.velocity.service.SysGeneratorService;
import com.gen.velocity.vo.TableSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author lijingyang
 * @Title:
 * @Package
 * @Description:
 * @date 2021/7/28 19:14
 */

@Api(value = "生成代码")
@Controller
@RequestMapping("/generator")
public class SysGeneratorController {
    @Autowired
    private SysGeneratorService sysGeneratorService;

    @AnnotationPrint(description = "跳转数据表页面")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String genList() {
        return "pages/gen/list";
    }

    @AnnotationPrint(description = "查询数据表")
    @RequestMapping(value = "/getTableDatas", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryTableList(@RequestBody TableSearchVO vo) {
        Map<String, Object> list = sysGeneratorService.queryList(vo);
        return list;
    }

    /**
     * 生成代码
     */
    @ApiOperation("生成代码")
    @GetMapping(value = "/code")
    public void code(@RequestParam List<String> tableNames,HttpServletResponse response) throws IOException {
        byte[] data = sysGeneratorService.generatorCode(tableNames);
        response.reset();
        response.setHeader("Content-Disposition","attachment; filename=\"x-springboot.zip\"");
        response.addHeader("Content-Length","" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data,response.getOutputStream());
    }
}